package com.cmcc.driver.ui.main.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cmcc.driver.data.DriverRequest
import com.cmcc.driver.data.entity.OrderTemp
import com.cmcc.driver.data.entity.OrderDetail
import com.cmcc.driver.data.entity.response.CarInfo
import com.cmcc.shared.config.SharedConfig
import com.cmcc.shared.model.DriverInfo
import com.cmcc.shared.utils.L
import com.google.android.gms.maps.model.LatLng
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainViewModel : ViewModel() {

    val driverInfo = MutableLiveData<DriverInfo>()


    val orderTemplate = MutableLiveData<OrderTemp>()
    var order = MutableLiveData<OrderDetail>()


    /**
     * 获取订单模板
     */
    fun getOrderTemplate(id: String): MutableLiveData<OrderTemp> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().getOrderTemplate(id)
                if (response.isSuccessful) {
                    val data = response.body()
                    orderTemplate.value = data?.data
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
        return orderTemplate
    }

    /**
     * 更新司机定位
     */
    fun updateLocation(currentLocation: LatLng) {
        viewModelScope.launch(Dispatchers.Main) {

            try {
                val response = DriverRequest.getInstance().updateLocation(currentLocation)
                if (response.isSuccessful) {

                } else {

                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    /**
     * 获取司机信息
     */
    fun getDriverInfo() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().getDriverInfo()
                if (response.isSuccessful) {
                    val data = response.body()
                    if (data?.code == 200) {
                        driverInfo.value = data.data
                    } else {
                        L.d("请求错误")
                    }
                } else {
                    L.d("服务器错误")
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }


    /**
     * 获取车辆信息及driverId
     */
    fun requestCarInfo() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().getCarInfo()
                if (response.isSuccessful) {
                    val body = response.body()
                    if (body?.data?.isNotEmpty() == true){
                        SharedConfig.cardId = body.data[0].idCard
                    }
                } else {
                    L.d("response failed")
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    /**
     * 查询正在进行的订单
     */
    fun queryOrderProgress() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().queryOrderInProgress()
                if (response.isSuccessful) {
                    val post = response.body()
                    order.value = post?.data
                } else {

                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

}